#include <iostream>
#include <thread>
#include <chrono>
#include "pub_sub/publisher.h"
#include "pub_sub/subscriber.h"
#include "pub_sub/pub_sub_infos.h"

struct TestMessage {
    int id;
    std::string content;
    
    TestMessage() : id(0) {}
    TestMessage(int i, const std::string& c) : id(i), content(c) {}
};

void demo_global_managers() {
    std::cout << "=== Pub/Sub 全局管理器演示 ===" << std::endl;
    
    // 创建发布者和订阅者
    {
        Publisher<TestMessage> pub("test_topic");
        Subscriber<TestMessage> sub("test_topic");
        
        // 订阅消息
        sub.Subscribe("test_topic", [](const TestMessage& msg) {
            std::cout << "收到消息: ID=" << msg.id << ", 内容=" << msg.content << std::endl;
        });
        
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        
        // 检查全局管理器状态
        auto& sub_manager = SubInfosManager::getInstance();
        auto& pub_manager = PubInfosManager::getInstance();
        
        std::cout << "活跃订阅者主题: ";
        for (const auto& topic : sub_manager.getActiveTopics()) {
            std::cout << topic << " ";
        }
        std::cout << std::endl;
        
        std::cout << "活跃发布者主题: ";
        for (const auto& topic : pub_manager.getActiveTopics()) {
            std::cout << topic << " ";
        }
        std::cout << std::endl;
        
        // 发布一些消息
        for (int i = 0; i < 5; ++i) {
            TestMessage msg(i, "测试消息 " + std::to_string(i));
            pub.Publish(msg);
            std::this_thread::sleep_for(std::chrono::milliseconds(50));
        }
        
        // 显示发布统计
        std::cout << "test_topic 发布计数: " 
                  << pub_manager.getPublishCount("test_topic") << std::endl;
        
    } // 析构函数会自动注销发布者和订阅者
    
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    // 检查清理后的状态
    auto& sub_manager = SubInfosManager::getInstance();
    auto& pub_manager = PubInfosManager::getInstance();
    
    std::cout << "清理后活跃订阅者数量: " << sub_manager.getActiveTopics().size() << std::endl;
    std::cout << "清理后活跃发布者数量: " << pub_manager.getActiveTopics().size() << std::endl;
}

void demo_multiple_topics() {
    std::cout << "\n=== 多主题管理演示 ===" << std::endl;
    
    // 创建多个主题的发布者和订阅者
    Publisher<TestMessage> pub1("topic1");
    Publisher<TestMessage> pub2("topic2");
    Publisher<TestMessage> pub3("topic3");
    
    Subscriber<TestMessage> sub1("topic1");
    Subscriber<TestMessage> sub2("topic2");
    
    // 订阅不同主题
    sub1.Subscribe("topic1", [](const TestMessage& msg) {
        std::cout << "Topic1 收到: " << msg.content << std::endl;
    });
    
    sub2.Subscribe("topic2", [](const TestMessage& msg) {
        std::cout << "Topic2 收到: " << msg.content << std::endl;
    });
    
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    auto& sub_manager = SubInfosManager::getInstance();
    auto& pub_manager = PubInfosManager::getInstance();
    
    std::cout << "当前活跃主题:" << std::endl;
    std::cout << "订阅者: ";
    for (const auto& topic : sub_manager.getActiveTopics()) {
        std::cout << topic << " ";
    }
    std::cout << std::endl;
    
    std::cout << "发布者: ";
    for (const auto& topic : pub_manager.getActiveTopics()) {
        std::cout << topic << " ";
    }
    std::cout << std::endl;
    
    // 发布到不同主题
    pub1.Publish(TestMessage(1, "来自topic1"));
    pub2.Publish(TestMessage(2, "来自topic2"));
    pub3.Publish(TestMessage(3, "来自topic3")); // topic3没有订阅者
    
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    // 显示各主题发布统计
    for (const auto& topic : pub_manager.getActiveTopics()) {
        std::cout << topic << " 发布计数: " 
                  << pub_manager.getPublishCount(topic) << std::endl;
    }
}

int main() {
    try {
        demo_global_managers();
        demo_multiple_topics();
        
        std::cout << "\n演示完成！" << std::endl;
        
    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}
